package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import dao.interfaces.ICommentDAO;
import model.Comment;

public class CommentDAO extends DAO<Comment> implements ICommentDAO<Comment> {
	
	public CommentDAO(Connection conn) {
		super(conn);
	}
	
	/**
	 * implements the inherited abstract methods from the DAO<User> abstract class
	 */
	
	@Override
	public boolean insert(Comment aComment) {
		String sql =
			"INSERT INTO cook_comment " +
			"(comment_title, comment_text, comment_date, user_id, recipe_id) " +
			"VALUES ('" + aComment.getTitle().replace("'", "''") + "', '" + 
			aComment.getText().replace("'", "''") + "',  DATE(NOW()), " + 
			aComment.getUser_id() + " ,  " + aComment.getRecipe_id() + ")";
		try {
			Statement stm = connection.createStatement();
			stm.executeUpdate(sql);
			stm.close();
			return true;
			} 
		catch (SQLException e) {
			e.printStackTrace();
			return false;
		}	
	}
	
	@Override
	public boolean delete(Comment obj) {
		return false;
	}
	
	@Override
	public boolean update(Comment obj) {
		return false;
	}
	
	@Override
	public Comment findById(int comment_id) {
		String sql =
			"SELECT * " +
			"FROM cook_comment " +
			"WHERE comment_id = " + comment_id;
		
		try {
			Statement statement = connection.createStatement();
			ResultSet rs = statement.executeQuery(sql);
			
			if (rs.next()) {
				Comment comment = new Comment();
				comment.setId(rs.getInt("comment_id"));
				comment.setTitle(rs.getString("comment_title"));
				comment.setText(rs.getString("comment_text"));
				comment.setDate(rs.getString("comment_date"));
				comment.setId(rs.getInt("user_id"));
				comment.setId(rs.getInt("recipe_id"));
				return comment;
			}			
		}
		catch (SQLException sqle) {
			System.out.println(sql);
			sqle.printStackTrace();
		}
		return null;
	}
	
	@Override
	public List<Comment> getAll() {
		List<Comment> commentList = new ArrayList<Comment>();
		try {
			Statement statement = connection.createStatement();
			ResultSet rs = statement.executeQuery("SELECT * FROM cook_comment ORDER BY comment_id DESC");
			
			while (rs.next()) {
				Comment comment = new Comment();
				comment.setId(rs.getInt("comment_id"));
				comment.setTitle(rs.getString("comment_title"));
				comment.setText(rs.getString("comment_text"));
				comment.setDate(rs.getString("comment_date"));
				comment.setUser_id(rs.getInt("user_id"));
				comment.setRecipe_id(rs.getInt("recipe_id"));
				commentList.add(comment);
			}
			if (commentList.size() > 0)
				return commentList;
		}
		catch (SQLException sqle) {
			sqle.printStackTrace();
		}
		return null;
	}	
	
	/**
	 * implements the inherited abstract methods from the ICommentDAO<Comment> interface
	 */
	
	@Override
	public List<Comment> getListBy(int id) {
		List<Comment> commentList = new ArrayList<Comment>();
		String sql =
			"SELECT * " +
			"FROM cook_comment " +
			"WHERE recipe_id = " + id + " ORDER BY comment_id DESC";
		
		try {
			Statement stm = connection.createStatement();
			ResultSet rs = stm.executeQuery(sql);
			
			while (rs.next()) {
				Comment comment = new Comment();
				comment.setId(rs.getInt("comment_id"));
				comment.setTitle(rs.getString("comment_title"));
				comment.setText(rs.getString("comment_text"));
				comment.setDate(rs.getString("comment_date"));
				comment.setUser_id(rs.getInt("user_id"));
				comment.setRecipe_id(rs.getInt("recipe_id"));
				commentList.add(comment);
			}
			if (commentList.size() > 0)
				return commentList;
		}
		catch (SQLException sqle) {
			sqle.printStackTrace();
		}
		return null;
	}	
}
